package com.gigi.searchcenterbackendmaster.model.dto;

import cn.hutool.core.bean.BeanUtil;
import com.gigi.searchcenterbackendmaster.model.entity.ArticleEntity;
import io.github.classgraph.json.Id;
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 用于接收 ES 中文章数据的 DTO
 *
 * @author Gigi
 * @date 2024/8/20 19:02
 */
@Document(indexName = "article") // 关联索引名称，也可以用别名
@Data
public class ArticleEsDTO {

    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

    @Id // 如果手动设置 id 值，则同时赋值给 _id
    private Long id;

    private String title;

    private String zhaiyao;

    private Integer ifDelete;

    @Field(index = false, store = true, type = FieldType.Date, format = {}, pattern = DATE_TIME_PATTERN)
    private Date createTime;

    private List<String> suggest;

    /**
     * 将实体类转换为 DTO
     *
     * @param articleEntity
     * @return
     */
    public static ArticleEsDTO entityToDTO(ArticleEntity articleEntity) {
        if (Objects.isNull(articleEntity)) {
            return null;
        }
        ArticleEsDTO articleEsDTO = new ArticleEsDTO();
        BeanUtil.copyProperties(articleEntity, articleEsDTO);
        articleEsDTO.setSuggest(org.elasticsearch.core.List.of(articleEntity.getTitle()));
        return articleEsDTO;
    }

}
